
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Django - Mr.Song</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="创建表字段123456789101112131415161718192021222324252627282930313233343536373839from django.db import mod,"> 
    <meta name="author" content="Mr.Song"> 
    
    <link rel="icon" href="/song_b/img/song.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/song_b/css/diaspora.css">

	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script>
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Mr.Song</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://gitee.com/song_shen_olympic/song_b.git"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">Django</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">Django</h1>
        <div class="stuff">
            <span>四月 21, 2020</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/song_b/tags/python/" rel="tag">python</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="创建表字段"><a href="#创建表字段" class="headerlink" title="创建表字段"></a>创建表字段</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">from django.db import models</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 导入时间域</span><br><span class="line">from django.utils import timezone</span><br><span class="line"></span><br><span class="line"># 基类</span><br><span class="line">class Base(models.Model):</span><br><span class="line"></span><br><span class="line">    #创建时间</span><br><span class="line">    create_time &#x3D; models.DateTimeField(default&#x3D;timezone.now,null&#x3D;True)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        abstract &#x3D; True</span><br><span class="line"></span><br><span class="line"># 用户表</span><br><span class="line">class User(Base):</span><br><span class="line"></span><br><span class="line">    #用户名</span><br><span class="line">    username &#x3D; models.CharField(max_length&#x3D;200)</span><br><span class="line"></span><br><span class="line">    #密码</span><br><span class="line">    password &#x3D; models.CharField(max_length&#x3D;200)</span><br><span class="line"></span><br><span class="line">    #头像</span><br><span class="line">    img &#x3D; models.CharField(max_length&#x3D;200)</span><br><span class="line"></span><br><span class="line">    #用户级别,0代表普通用户，1代表超级管理员，2网站编辑</span><br><span class="line">    usertype &#x3D; models.IntegerField(default&#x3D;0,null&#x3D;True)</span><br><span class="line"></span><br><span class="line">    #手机号</span><br><span class="line">    phone &#x3D; models.IntegerField()</span><br><span class="line"></span><br><span class="line">    #个人主页</span><br><span class="line">    num &#x3D; models.IntegerField(default&#x3D;0,null&#x3D;True)</span><br><span class="line"></span><br><span class="line">    #声明表名</span><br><span class="line">    class Meta:</span><br><span class="line">        db_table &#x3D; &#39;user&#39;</span><br></pre></td></tr></table></figure>

<h1 id="数据库迁移"><a href="#数据库迁移" class="headerlink" title="数据库迁移"></a>数据库迁移</h1><p>python manage.py makemigrations 项目名<br>python manage.py makemigrations myapp<br>注意：一定要加项目名称，不然myapp中models.py中的表就迁移不到数据库中<br>然后执行：python manage.py migrate</p>
<h5 id="在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels-py中，命令：（python-manage-py-inspectdb-gt-app01-models-py"><a href="#在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels-py中，命令：（python-manage-py-inspectdb-gt-app01-models-py" class="headerlink" title="在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01/models.py"></a>在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01/models.py</h5><p>）</p>
<h1 id="导包时注意事项"><a href="#导包时注意事项" class="headerlink" title="导包时注意事项"></a>导包时注意事项</h1><p>from 项目名.models import User<br>from myapp.models import User<br>在哪个项目中就用哪个项目名</p>
<h1 id="md5加密"><a href="#md5加密" class="headerlink" title="md5加密"></a>md5加密</h1><p>要将需要加密的变量进行强转，强转成字符串，然后进行编码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># md5加密方法</span><br><span class="line">def make_pasword(mypass):</span><br><span class="line"></span><br><span class="line">    #生成md5对象</span><br><span class="line">    md5 &#x3D; hashlib.md5()</span><br><span class="line"></span><br><span class="line">    #转码操作</span><br><span class="line">    mypass_utf8 &#x3D; str(mypass).encode(encoding&#x3D;&#39;utf-8&#39;)</span><br><span class="line"></span><br><span class="line">    #进行加密操作</span><br><span class="line">    md5.update(mypass_utf8)</span><br><span class="line"></span><br><span class="line">    #返回密文</span><br><span class="line">    return md5.hexdigest()</span><br></pre></td></tr></table></figure>



<p><img src="/hugo_blog/0d8a90abc2d78eaa5e01d3fb7f57e6da.jpg" alt="美图"></p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="true">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='/song_b//img/002.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci=''
        data-cs=''
        data-r=''
        data-o=''
        data-a=''
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#创建表字段"><span class="toc-number">1.</span> <span class="toc-text">创建表字段</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#数据库迁移"><span class="toc-number">2.</span> <span class="toc-text">数据库迁移</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels-py中，命令：（python-manage-py-inspectdb-gt-app01-models-py"><span class="toc-number">2.0.0.0.1.</span> <span class="toc-text">在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01&#x2F;models.py</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#导包时注意事项"><span class="toc-number">3.</span> <span class="toc-text">导包时注意事项</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#md5加密"><span class="toc-number">4.</span> <span class="toc-text">md5加密</span></a></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/song_b/js/plugin.js"></script>
<script src="/song_b/js/typed.js"></script>
<script src="/song_b/js/diaspora.js"></script>


<link rel="stylesheet" href="/song_b/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/song_b/photoswipe/default-skin/default-skin.css">


<script src="/song_b/photoswipe/photoswipe.min.js"></script>
<script src="/song_b/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
